package com.kele.file.controller;

import com.aliyun.oss.OSS;
import com.kele.common.model.response.ResultObject;
import com.kele.file.config.FastDFSClient;
import com.kele.file.model.FileInfo;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;

@RestController
@RequestMapping("upload")
@CrossOrigin
public class UploadController {

    @Autowired
    FastDFSClient fastDFSClient;

    @Value("${oss.bucket}")
    String bucket;
    @Value("${oss.hostName}")//https://1905a-1.oss-cn-beijing.aliyuncs.com
    String hostName;

    @Autowired
    OSS oss;

    @PostMapping("native")
    public ResultObject<FileInfo> upload_native(@RequestParam("file") MultipartFile file) throws IOException {

        String originalFilename = file.getOriginalFilename();
        int dot_index = originalFilename.lastIndexOf(".");
        String fileExt = originalFilename.substring(dot_index + 1);
        String name = file.getName();
        String contentType = file.getContentType();
        long size = file.getSize();

        File dir = new File("d:\\01_1905A\\kele_mall\\images_1905A");
        if (!dir.exists()) {
            dir.mkdir();
        }
        File fileDest = new File(dir, originalFilename);

        file.transferTo(fileDest);//保存到。。。

        String url = "http://localhost:18087/images111/" + originalFilename;
        FileInfo fileInfo = new FileInfo(originalFilename, fileExt, size, contentType, url, new Date());
        return new ResultObject<>(true, "1", "上传成功", fileInfo);
    }

    @PostMapping("fastdfs")
    public ResultObject<FileInfo> upload_fastdfs(@RequestParam("file") MultipartFile file) throws IOException {

        String fileName = file.getOriginalFilename();
        String web_server_file_url = fastDFSClient.upload(file);
        FileInfo fileInfo = new FileInfo(fileName,
                FilenameUtils.getExtension(fileName),
                file.getSize(),
                file.getContentType(),
                web_server_file_url, new Date());
        return new ResultObject<>(true, "1", "上传成功", fileInfo);

    }

    @PostMapping("oss")
    public ResultObject<FileInfo> upload_oss(@RequestParam("file") MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        /**
         * String bucketName,
         * String key, 文件名
         * InputStream input
         */
        oss.putObject(bucket,file.getOriginalFilename(),file.getInputStream());

        String bucket_url= hostName + "/" + file.getOriginalFilename();

        FileInfo fileInfo = new FileInfo(fileName,
                FilenameUtils.getExtension(fileName),
                file.getSize(),
                file.getContentType(),
                bucket_url, new Date());
        return new ResultObject<>(true, "1", "上传成功", fileInfo);

    }
}
